home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / zdps1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  9.3 KB  |  383 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zdps1.c */
  20. /* Display PostScript graphics extensions */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"
  25. #include "gspath.h"
  26. #include "gsstate.h"
  27. #include "alloc.h"
  28. #include "ivmspace.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "stream.h"
  32. #include "bnum.h"
  33.  
  34. /* Imported data */
  35. extern op_proc_p zcopy_procs[t_next_index];
  36.  
  37. /* Forward references */
  38. private int zcopy_gstate(P1(os_ptr));
  39.  
  40. #ifdef AMIGA
  41. /* External references */
  42. extern int gs_rectappend(gs_state *, const gs_rect *, uint);
  43. extern int gs_rectclip(gs_state *, const gs_rect *, uint);
  44. extern int gs_rectfill(gs_state *, const gs_rect *, uint);
  45. extern int gs_rectstroke(gs_state *, const gs_rect *, uint, const gs_matrix *);
  46. extern int gs_setbbox(gs_state *, floatp, floatp, floatp, floatp);
  47. #endif
  48.  
  49. /* Initialize by adding an entry for gstates to the `copy' operator. */
  50. private void
  51. zdps1_init(void)
  52. {    zcopy_procs[t_gstate] = zcopy_gstate;
  53. }
  54.  
  55. /* ------ Graphics state ------ */
  56.  
  57. /* <bool> setstrokeadjust - */
  58. int
  59. zsetstrokeadjust(register os_ptr op)
  60. {    check_type(*op, t_boolean);
  61.     gs_setstrokeadjust(igs, op->value.index);
  62.     pop(1);
  63.     return 0;
  64. }
  65.  
  66. /* - currentstrokeadjust <bool> */
  67. int
  68. zcurrentstrokeadjust(register os_ptr op)
  69. {    push(1);
  70.     make_bool(op, gs_currentstrokeadjust(igs));
  71.     return 0;
  72. }
  73.  
  74. /* <x> <y> sethalftonephase - */
  75. int
  76. zsethalftonephase(register os_ptr op)
  77. {    int code;
  78.     long x, y;
  79.     check_type(op[-1], t_integer);
  80.     check_type(*op, t_integer);
  81.     x = op[-1].value.intval;
  82.     y = op->value.intval;
  83.     if ( x != (int)x || y != (int)y )
  84.         return_error(e_rangecheck);
  85.     code = gs_sethalftonephase(igs, (int)x, (int)y);
  86.     if ( code >= 0 ) pop(2);
  87.     return code;
  88. }
  89.  
  90. /* - currenthalftonephase <x> <y> */
  91. int
  92. zcurrenthalftonephase(register os_ptr op)
  93. {    gs_int_point phase;
  94.     gs_currenthalftonephase(igs, &phase);
  95.     push(2);
  96.     make_int(op - 1, phase.x);
  97.     make_int(op, phase.y);
  98.     return 0;
  99. }
  100.  
  101. /* ------ Graphics state objects ------ */
  102.  
  103. /* Check to make sure that all the elements of a graphics state */
  104. /* are global.  ****** DOESN'T CHECK THE NON-REFS. ****** */
  105. private int
  106. gstate_check_global(int_gstate *isp)
  107. {
  108. #define gsref_check(p) check_global(*(p))
  109.     int_gstate_map_refs(isp, gsref_check);
  110. #undef gsref_check
  111.     return 0;
  112. }
  113.  
  114. /* - gstate <gstate> */
  115. int
  116. zgstate(register os_ptr op)
  117. {    gs_state *pnew;
  118.     int_gstate *isp;
  119.     if ( !alloc_current_local() )
  120.     {    int code = gstate_check_global(&istate);
  121.         if ( code < 0 ) return code;
  122.     }
  123.     pnew = gs_gstate(igs);
  124.     if ( pnew == 0 )
  125.         return_error(e_VMerror);
  126.     isp = (int_gstate *)gs_state_client_data(pnew);
  127.     int_gstate_map_refs(isp, ref_mark_new);
  128.     push(1);
  129.     make_tv(op, t_gstate, pgstate, pnew);
  130.     return 0;
  131. }
  132.  
  133. /* copy for gstates */
  134. private int
  135. zcopy_gstate(register os_ptr op)
  136. {    os_ptr op1 = op - 1;
  137.     gs_state *pgs;
  138.     gs_state *pgs1;
  139.     int_gstate *pistate;
  140.     int code;
  141.     check_type(*op, t_gstate);
  142.     check_type(*op1, t_gstate);
  143.     pgs = op->value.pgstate;
  144.     pgs1 = op1->value.pgstate;
  145.     pistate = (int_gstate *)gs_state_client_data(pgs);
  146.     if ( !r_is_global(op) )
  147.     {    code = gstate_check_global((int_gstate *)gs_state_client_data(pgs1));
  148.         if ( code < 0 ) return code;
  149.     }
  150. #define gsref_save(p) ref_save(p, "currentgstate")
  151.     int_gstate_map_refs(pistate, gsref_save);
  152. #undef gsref_save
  153.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  154.     code = gs_copygstate(pgs, pgs1);
  155.     if ( code < 0 ) return code;
  156.     int_gstate_map_refs(pistate, ref_mark_new);
  157.     *op1 = *op;
  158.     pop(1);
  159.     return 0;
  160. }
  161.  
  162. /* <gstate> currentgstate <gstate> */
  163. int
  164. zcurrentgstate(register os_ptr op)
  165. {    int code;
  166.     int_gstate *pistate;
  167.     check_type(*op, t_gstate);
  168.     pistate = (int_gstate *)gs_state_client_data(op->value.pgstate);
  169.     if ( !r_is_global(op) )
  170.     {    code = gstate_check_global(&istate);
  171.         if ( code < 0 ) return code;
  172.     }
  173. #define gsref_save(p) ref_save(p, "currentgstate")
  174.     int_gstate_map_refs(pistate, gsref_save);
  175. #undef gsref_save
  176.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  177.     code = gs_currentgstate(op->value.pgstate, igs);
  178.     if ( code < 0 ) return code;
  179.     int_gstate_map_refs(pistate, ref_mark_new);
  180.     return 0;
  181. }
  182.  
  183. /* <gstate> setgstate - */
  184. int
  185. zsetgstate(register os_ptr op)
  186. {    int code;
  187.     check_type(*op, t_gstate);
  188.     code = gs_setgstate(igs, op->value.pgstate);
  189.     if ( code < 0 ) return code;
  190.     pop(1);
  191.     return 0;
  192. }
  193.  
  194. /* ------ Rectangles ------- */
  195.  
  196. /* We preallocate a short list for rectangles, because */
  197. /* the rectangle operators usually will involve very few rectangles. */
  198. #define max_local_rect 5
  199. typedef struct local_rects_s {
  200.     gs_rect *pr;
  201.     uint count;
  202.     gs_rect rl[max_local_rect];
  203. } local_rects;
  204.  
  205. /* Forward references */
  206. private int rect_get(P2(local_rects *, os_ptr));
  207. private void rect_release(P1(local_rects *));
  208.  
  209. /* <x> <y> <width> <height> rectappend - */
  210. /* <numarray|numstring> rectappend - */
  211. int
  212. zrectappend(os_ptr op)
  213. {    local_rects lr;
  214.     int npop = rect_get(&lr, op);
  215.     int code;
  216.     if ( npop < 0 ) return npop;
  217.     code = gs_rectappend(igs, lr.pr, lr.count);
  218.     rect_release(&lr);
  219.     if ( code < 0 ) return code;
  220.     pop(npop);
  221.     return 0;
  222. }
  223.  
  224. /* <x> <y> <width> <height> rectclip - */
  225. /* <numarray|numstring> rectclip - */
  226. int
  227. zrectclip(os_ptr op)
  228. {    local_rects lr;
  229.     int npop = rect_get(&lr, op);
  230.     int code;
  231.     if ( npop < 0 ) return npop;
  232.     code = gs_rectclip(igs, lr.pr, lr.count);
  233.     rect_release(&lr);
  234.     if ( code < 0 ) return code;
  235.     pop(npop);
  236.     return 0;
  237. }
  238.  
  239. /* <x> <y> <width> <height> rectfill - */
  240. /* <numarray|numstring> rectfill - */
  241. int
  242. zrectfill(os_ptr op)
  243. {    local_rects lr;
  244.     int npop = rect_get(&lr, op);
  245.     int code;
  246.     if ( npop < 0 ) return npop;
  247.     code = gs_rectfill(igs, lr.pr, lr.count);
  248.     rect_release(&lr);
  249.     if ( code < 0 ) return code;
  250.     pop(npop);
  251.     return 0;
  252. }
  253.  
  254. /* <x> <y> <width> <height> rectstroke - */
  255. /* <numarray|numstring> rectstroke - */
  256. int
  257. zrectstroke(os_ptr op)
  258. {    gs_matrix mat;
  259.     local_rects lr;
  260.     int npop, code;
  261.     if ( read_matrix(op, &mat) >= 0 )
  262.        {    /* Concatenate the matrix to the CTM just before */
  263.         /* stroking the path. */
  264.         npop = rect_get(&lr, op - 1);
  265.         if ( npop < 0 ) return npop;
  266.         code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  267.         npop++;
  268.        }
  269.     else
  270.        {    /* No matrix. */
  271.         npop = rect_get(&lr, op);
  272.         if ( npop < 0 ) return npop;
  273.         code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *)0);
  274.        }
  275.     rect_release(&lr);
  276.     if ( code < 0 ) return code;
  277.     pop(npop);
  278.     return 0;
  279. }
  280.  
  281. /* --- Internal routines --- */
  282.  
  283. /* Get rectangles from the stack. */
  284. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  285. private int
  286. rect_get(local_rects *plr, os_ptr op)
  287. {    int code, npop;
  288.     stream st;
  289.     uint n, count;
  290.     gs_rect *pr;
  291.     switch ( r_type(op) )
  292.        {
  293.     case t_array:
  294.     case t_string:
  295.         code = sread_num_array(&st, op);
  296.         if ( code < 0 ) return code;
  297.         count = scount_num_stream(&st);
  298.         if ( count % 4 )
  299.             return_error(e_typecheck);
  300.         count /= 4, npop = 1;
  301.         break;
  302.     default:            /* better be 4 numbers */
  303.         sread_string(&st, (byte *)(op - 3), sizeof(ref) * 4);
  304.         st.num_format = num_array;
  305.         count = 1, npop = 4;
  306.         break;
  307.        }
  308.     plr->count = count;
  309.     if ( count <= max_local_rect )
  310.         pr = plr->rl;
  311.     else
  312.        {    pr = (gs_rect *)alloc(count, sizeof(gs_rect), "rect_get");
  313.         if ( pr == 0 )
  314.             return_error(e_VMerror);
  315.        }
  316.     plr->pr = pr;
  317.     for ( n = 0; n < count; n++, pr++ )
  318.        {    ref rnum;
  319.         float rv[4];
  320.         int i;
  321.         for ( i = 0; i < 4; i++ )
  322.            {    switch ( code = sget_encoded_number(&st, &rnum) )
  323.                {
  324.             case t_integer:
  325.                 rv[i] = rnum.value.intval;
  326.                 break;
  327.             case t_real:
  328.                 rv[i] = rnum.value.realval;
  329.                 break;
  330.             default:    /* code < 0 */
  331.                 return code;
  332.                }
  333.            }
  334.         pr->q.x = (pr->p.x = rv[0]) + rv[2];
  335.         pr->q.y = (pr->p.y = rv[1]) + rv[3];
  336.        }
  337.     return npop;
  338. }
  339.  
  340. /* Release the rectangle list if needed. */
  341. private void
  342. rect_release(local_rects *plr)
  343. {    if ( plr->pr != plr->rl )
  344.         alloc_free((char *)plr->pr, plr->count, sizeof(gs_rect),
  345.                "rect_release");
  346. }
  347.  
  348. /* ------ Graphics state ------ */
  349.  
  350. /* <llx> <lly> <urx> <ury> setbbox - */
  351. int
  352. zsetbbox(register os_ptr op)
  353. {    float box[4];
  354.     int code = num_params(op, 4, box);
  355.     if ( code < 0 ) return code;
  356.     if ( (code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0 )
  357.         return code;
  358.     pop(4);
  359.     return 0;
  360. }
  361.  
  362. /* ------ Initialization procedure ------ */
  363.  
  364. op_def zdps1_op_defs[] = {
  365.         /* Graphics state */
  366.     {"0currenthalftonephase", zcurrenthalftonephase},
  367.     {"0currentstrokeadjust", zcurrentstrokeadjust},
  368.     {"2sethalftonephase", zsethalftonephase},
  369.     {"1setstrokeadjust", zsetstrokeadjust},
  370.         /* Graphics state objects */
  371.     {"1currentgstate", zcurrentgstate},
  372.     {"0gstate", zgstate},
  373.     {"1setgstate", zsetgstate},
  374.         /* Rectangles */
  375.     {"1rectappend", zrectappend},
  376.     {"1rectclip", zrectclip},
  377.     {"1rectfill", zrectfill},
  378.     {"1rectstroke", zrectstroke},
  379.         /* Graphics state components */
  380.     {"4setbbox", zsetbbox},
  381.     op_def_end(zdps1_init)
  382. };
  383.